#!/usr/bin/python
import os
import sys
from operator import itemgetter, attrgetter

# Parameters.
total_file = sys.argv[1]
sum_file = sys.argv[2]

# Read in totals.
totals = {}
fin = open(total_file, "rb")
for line in fin:
	tmp = line.strip().split()
	if len(tmp) != 2:
		continue
	totals[tmp[1]] = float(tmp[0])
fin.close()

# Open summary.
max = {}
batch = {}
hits = {}
fin = open(sum_file, "rb")
for line in fin:
	# Split.
	tmp = line.strip().split()
	
	# Determine batch.
	if len(tmp) == 1:
		batch[tmp[0]] = {}
		hits[tmp[0]] = {}
		cur = batch[tmp[0]]
		cur2 = hits[tmp[0]]
		continue
		
	# Add hits to batch.
	rela = float(tmp[0]) / totals[tmp[1]] 
	cur[tmp[1]] = rela
	cur2[tmp[1]] = tmp[0]
	
	# See if affects max.
	if tmp[1] not in max:
		max[tmp[1]] = -1
	if rela > max[tmp[1]]:
		max[tmp[1]] = rela
	

# Sort by max.
srt = []
for m in max:
	srt.append((max[m], m))
srt = sorted(srt, key=itemgetter(0))
srt.reverse()

# Print out headers.
tmp = ""
for n in batch:
	tmp += "\t\t" + n
print tmp

# Print out summary.
for m in srt:
	# Loop over files.
	tmp = "%s\t%i\t" % (m[1], totals[m[1]])
	for n in batch:
		# data
		if m[1] in batch[n]:
			tmp += "\t" + str(batch[n][m[1]])
		else:
			tmp += "\t0.0"
			
	for n in batch:
		# data
		if m[1] in batch[n]:
			tmp += "\t" + hits[n][m[1]]
		else:
			tmp += "\t0.0"
		
	print tmp

